﻿#ifndef GUARD_H
#define GUARD_H

namespace RemoteObj
{
template <typename LockType>
class Guard
{
public:
    Guard(LockType* lock) : _locked(false), _lock(lock)
    {
        if (NULL != _lock)
        {
            if (0 == _lock->lock())
            {
                _locked = true;
            }
        }
    }

    ~Guard()
    {
        if (NULL != _lock)
        {
            if (0 == _lock->unlock())
            {
                _locked = false;
            }
        }
    }

    bool is_locked() const
    {
        return _locked;
    }

private:
    mutable bool _locked;
    LockType* _lock;
};

}

#endif // GUARD_H
